<?php
/**
* @file Jfpointsm.php
* @synopsis  更新积分日志
* @author Yee, <rlk002@gmail.com>
* @version 1.0
* @date 2015年7月7日16:32:43
*/


class Jfpointsm extends CI_Model
{
    const RELATED_TYPE_INVITE_SEND = 100;
    const RELATED_TYPE_INVITE_SEND_CANCEL = 101;
    const RELATED_TYPE_INVITE_ACCEPT = 102;
    const RELATED_TYPE_INVITE_ACCEPT_CANCEL = 103;
    const RELATED_TYPE_MARKED_PUSH = 104;
    const RELATED_TYPE_REALNAME_POST = 105;
    const RELATED_TYPE_REALNAME_POST_CANCEL = 106;
    const RELATED_TYPE_BESTANSWER_SET = 107;
    const RELATED_TYPE_BESTANSWER_CANCEL = 108;
    const RELATED_TYPE_POST = 109;
    const RELATED_TYPE_POST_CANCEL = 110;
    const RELATED_JFPOINT_INVITE_REGISTER = 100;
    const RELATED_JFPOINT_REGISTER = 30;


    const SHARE_POST_COMPLETE_JF = 50; //分享病例帖子  公布答案
    const SHARE_POST_JFPOINT = 5; //分享帖子5积分
    const SHARE_POST_PART = 10; //分享图谱10积分
    const SHARE_POST_DISUBJECT = 1; //分享专题1积分

    function __construct()
    {
        $this->parttime = $this->load->database('parttime', TRUE);
        $config = $this->config->item('jfpoints');
    }

    //下载减积分
    public function part_download($uid, $part_id)
    {
        //获取用户积分
        $uid_jfpoint = (float)$this->get_uid_jfpoints($uid);
        $part_jfpoint = (float)$this->get_part_jfpoints($part_id);

        //获取需要消耗的积分
        if ($part_jfpoint > 0) {
            if ($uid_jfpoint < $part_jfpoint || !$uid_jfpoint) {
                $result = array("code" => -2, 'tips' => "您的积分不够");
            } else {
                $jfdata = array();
                $jfdata['adduser'] = $uid;
                //$jfdata['dealuser'] = ADMINUSERID;
                $jfdata['beforejfpoints'] = $uid_jfpoint;
                $jfdata['jfpoints'] = -$part_jfpoint;
                $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] - $part_jfpoint;
                $jfdata['addtime'] = time();
                $jfdata['stage'] = "图谱下载";
                $jfdata['desc'] = $part_id . "图谱消耗";

                $jfpoint_data = $this->insert_jfpoints($jfdata);
                if($jfpoint_data)
                {
                    $return = ['code'=>1,'cost'=>$part_jfpoint];
                }
                else
                {
                    $return = ['code'=>-3,'tips' => "扣除积分失败,请重试"];
                }
            }
        } else {
            //如果是0积分 直接下载
            $return = ['code'=>1,'cost'=>0];
        }
        return $return;

    }

    //被赞一次加1个积分  取消不减分
    public function get_comment_good($uid, $comment_id)
    {
        //认证用户点赞才给积分
        $this->load->model('userm');
        $uid_info = $this->userm->get_one_user_4_search($uid, $uid);
        $uid_verified_status = $uid_info['verified_status'];
        //到了这里 只管点赞用户是否认证
        if ($uid_verified_status == 3) {
            //获取评论的作者
            $this->load->model('commentm');
            $comment_author_id = $this->commentm->get_comment_author_id($comment_id);
            //获取用户积分
            $uid_jfpoint = $this->get_uid_jfpoints($comment_author_id);

            $jfdata = array();
            $jfdata['adduser'] = $comment_author_id;
            //$jfdata['dealuser'] = ADMINUSERID;
            $jfdata['beforejfpoints'] = $uid_jfpoint;
            $jfdata['jfpoints'] = 1;
            $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] + 1;
            $jfdata['addtime'] = time();
            $jfdata['stage'] = "评论被赞";
            $jfdata['desc'] = "评论被赞";

            $jfpoint_data = $this->insert_jfpoints($jfdata);
            return true;

        }
        else
        {
            return false;
        }

    }

    /*//获取用户积分
    public function get_jfpoints_count()
    {
        $result = $this->db->select("sum(jfpoints) as sum")->where("adduser",$user_id)->get("ml_jfpoint_log")->result_array();
        return $result['sum'];
    }*/

    //更新积分日志
    public function insert_jfpoints($query)
    {
        return $this->db->insert("ml_jfpoint_log", $query);
    }

    //获取用户积分
    public function get_uid_jfpoints($uid)
    {
        if(empty($uid))
        {
            return 0;
        }
        $result = $this->db->query("select resultjfpoints from  ml_jfpoint_log where adduser = ".$uid." order by id desc limit 1")->result_array();
        //$result = $this->db->where("adduser", $uid)->order_by("id", "desc")->limit(1, 0)->get("ml_jfpoint_log")->result_array();  //报错  Call to a member function result_array() on boolean

        return $result && $result[0] ? $result[0]["resultjfpoints"] : 0;
    }

    //获取用户积分记录
    public function get_jf_records_by_stage($uid, $stage, $page = 1, $page_size = 20)
    {
        if (empty($stage)) {
            return array();
        }
        $whe = array(
            "adduser" => $uid,
            "stage" => $stage
        );
        $start = ($page - 1) * $page_size;
        $result = $this->db->select("resultjfpoints")->where($whe)->order_by("id", "desc")->limit($page_size, $start)->get("ml_jfpoint_log")->result_array();

        return $result;
    }


    //获取图片下载所需积分
    public function get_part_jfpoints($part_id)
    {
        $result = $this->db->select("free_limit_start_time,free_limit_end_time,free_limit_jfpoints,jfpoints")->where("part_id", $part_id)->get("ml_part")->result_array();
        $now_time = time();
        if ($result[0]['free_limit_start_time'] && $result[0]['free_limit_end_time'] && ($result[0]['free_limit_start_time'] < $now_time && $now_time < $result[0]['free_limit_end_time'])) {
            $return = $result[0]['free_limit_jfpoints'];
        } else {
            $return = $result[0]['jfpoints'];
        }
        return $return;
    }


    //注册送30个积分
    public function add_register($uid, $invite_code = '')
    {
        //活动期间 邀请注册送3个积分
        $start_time = strtotime("2016-01-21 19:00:00");
        $end_time = strtotime("2016-02-14 00:00:00");
        $now_time = time();
        if($now_time > $start_time and $now_time < $end_time)
        {
            $add_reg_jfpoints = 50;
        }
        else
        {
            $add_reg_jfpoints = self::RELATED_JFPOINT_INVITE_REGISTER;
        }

        $uid_jfpoint = 0;
        //获取用户积分
        $jfdata = array();
        $jfdata['adduser'] = $uid;
        //$jfdata['dealuser'] = ADMINUSERID;
        $jfdata['beforejfpoints'] = $uid_jfpoint;
        $jfdata['jfpoints'] = self::RELATED_JFPOINT_REGISTER;
        $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] + self::RELATED_JFPOINT_REGISTER;
        $jfdata['addtime'] = time();
        $jfdata['stage'] = "用户注册";
        $jfdata['desc'] = "用户注册";

        $jfpoint_data = $this->insert_jfpoints($jfdata);


        //如果有邀请码  医院推广 不参与活动
        if ($invite_code) {
            $this->load->model("invitecodem");
            $invite_code_owned = $this->invitecodem->get_invite_code_owned($invite_code);  //如果没有说明是市场推广用户 不给积分


            if ($invite_code_owned) {
                //$uid_jfpoint = $this->get_uid_jfpoints($uid);
                $uid_jfpoint = self::RELATED_JFPOINT_REGISTER;
                //获取邀请积分
                $jfdata = array();
                $jfdata['adduser'] = $uid;
                //$jfdata['dealuser'] = ADMINUSERID;
                $jfdata['beforejfpoints'] = $uid_jfpoint;
                $jfdata['jfpoints'] = self::RELATED_JFPOINT_INVITE_REGISTER;
                $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] + self::RELATED_JFPOINT_INVITE_REGISTER;;
                $jfdata['addtime'] = time();
                $jfdata['stage'] = "被邀请用户";
                $jfdata['desc'] = "被邀请用户";
                $jfpoint_data = $this->insert_jfpoints($jfdata);

            } elseif (!$invite_code_owned && strlen($invite_code) == 4) {
                //如果是医院推广也给积分  但是不参与活动
                $yiyuan_result = $this->parttime->query("select count(a.id) as count from pt_user as a left join pt_role as b on a.role_id=b.role_id where a.invite_code='" . $invite_code . "' and b.qudao_id=2")->result_array();
                $invite_code_owned = $yiyuan_result[0]['count'];

                if ($invite_code_owned) {
                    //注册成功就是30积分 不用  再去查数据库了
                    $uid_jfpoint = 30;
                    //获取邀请积分
                    $jfdata = array();
                    $jfdata['adduser'] = $uid;
                    //$jfdata['dealuser'] = ADMINUSERID;
                    $jfdata['beforejfpoints'] = $uid_jfpoint;
                    $jfdata['jfpoints'] = self::RELATED_JFPOINT_INVITE_REGISTER;
                    $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] + self::RELATED_JFPOINT_INVITE_REGISTER;
                    $jfdata['addtime'] = time();
                    $jfdata['stage'] = "医院推广邀请用户";
                    $jfdata['desc'] = "医院推广邀请用户";
                    $jfpoint_data = $this->insert_jfpoints($jfdata);
                }


            }


        }
        return $jfpoint_data;
    }


    //三级版主推精 增加10个积分
    public function add_digest($uid)
    {
        //提高版主推精积分  20积分改为30积分  2016-02-25 李晓阳
        $new_digest_jfpoints = 50;
        //获取用户积分
        $uid_jfpoint = $this->get_uid_jfpoints($uid);

        $jfdata = array();
        $jfdata['adduser'] = $uid;
        //$jfdata['dealuser'] = ADMINUSERID;
        $jfdata['beforejfpoints'] = $uid_jfpoint;
        $jfdata['jfpoints'] = $new_digest_jfpoints;
        $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] + $new_digest_jfpoints;
        $jfdata['addtime'] = time();
        $jfdata['stage'] = "版主推精";
        $jfdata['desc'] = "版主推精";

        $jfpoint_data = $this->insert_jfpoints($jfdata);
        return $jfpoint_data;
    }

    //不同的分享渠道 积分不一样
    public function share_jfpoint($uid,$resource_name='',$resource_id=0)
    {
        if($resource_name == 'post')
        {
            $share_jfpoint = self::SHARE_POST_JFPOINT;
        }
        elseif($resource_name == 'disubject')
        {
            $share_jfpoint = self::SHARE_DISUBJECT_JFPOINT;
        }
        elseif($resource_name == 'part')
        {
            $share_jfpoint = self::SHARE_PART_JFPOINT;
        }
        //获取用户积分
        $uid_jfpoint = $this->get_uid_jfpoints($uid);
        //增加一个描述  分享的类型

            $share_desc = $resource_name."_".$resource_id;

        $jfdata = array();
        $jfdata['adduser'] = $uid;
        //$jfdata['dealuser'] = ADMINUSERID;
        $jfdata['beforejfpoints'] = $uid_jfpoint;
        $jfdata['jfpoints'] = $share_jfpoint;
        $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] + $share_jfpoint;
        $jfdata['addtime'] = time();
        $jfdata['stage'] = "分享";
        $jfdata['desc'] = "分享得积分".$share_desc;

        $jfpoint_data = $this->insert_jfpoints($jfdata);
        return $jfpoint_data;
    }

    /**
     * 公布答案，增加5积分
     * @param $uid
     * @return mixed
     */
    public function published_answer_add_jf($uid, $post_id)
    {
        //获取用户积分
        $uid_jfpoint = $this->get_uid_jfpoints($uid);
        $jfdata = array();
        $jfdata['adduser'] = $uid;
        //$jfdata['dealuser'] = ADMINUSERID;
        $jfdata['beforejfpoints'] = $uid_jfpoint;
        $jfdata['jfpoints'] = 5;
        $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] + 5;
        $jfdata['addtime'] = time();
        $jfdata['stage'] = "公布答案";
        $jfdata['desc'] = "公布答案增加5积分";
        $jfdata['related_id'] = $post_id;
        $jfpoint_data = $this->insert_jfpoints($jfdata);
        return $jfpoint_data;
    }

    /**
     * 分享贴公布答案，增加50积分  发布完整分享病例  频率不限,公布答案时发放
     * @param $uid
     * @return mixed
     */
    public function share_post_complete_jf($uid, $post_id)
    {

        //获取用户积分
        $uid_jfpoint = $this->get_uid_jfpoints($uid);
        $jfdata = array();
        $jfdata['adduser'] = $uid;
        //$jfdata['dealuser'] = ADMINUSERID;
        $jfdata['beforejfpoints'] = $uid_jfpoint;
        $jfdata['jfpoints'] = self::SHARE_POST_COMPLETE_JF;
        $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] + self::SHARE_POST_COMPLETE_JF;
        $jfdata['addtime'] = time();
        $jfdata['stage'] = "发布完整分享病例";
        $jfdata['desc'] = "发布完整分享病例增加50积分";
        $jfdata['related_id'] = $post_id;
        $jfpoint_data = $this->insert_jfpoints($jfdata);
        return $jfpoint_data;
    }

    /**
     * 公共更改积分
     * @param $uid              用户ID
     * @param $stage            描述简写
     * @param $desc             描述
     * @param int $points       积分 正数加积分 负数减积分
     */
    public function change_point($uid, $stage, $desc, $points = 5,  $related_id = 0)
    {
        //获取用户积分
        $uid_jfpoint = $this->get_uid_jfpoints($uid);

        $resultjfpoints = $uid_jfpoint + $points;

        $jfdata = array();
        $jfdata['adduser']          = $uid;
        $jfdata['beforejfpoints']   = $uid_jfpoint;
        $jfdata['jfpoints']         = $points;
        $jfdata['resultjfpoints']   = $resultjfpoints > 0 ? $resultjfpoints : 0;
        $jfdata['addtime']          = time();
        $jfdata['stage']            = $stage;
        $jfdata['desc']             = $desc;
        $jfdata['related_id']       = $related_id;
        $jfpoint_data = $this->insert_jfpoints($jfdata);
        return $jfpoint_data;
    }


    /**
     * 获取当天用户 邀请回答获得的总积分
     * @param $uid
     */
    public function get_today_invite_jf($uid)
    {
        $start_time = strtotime(date('Y-m-d').' 00:00:00');
        $end_time   = strtotime(date('Y-m-d').' 23:59:59');
        $add_count = $this->db->select('jfpoints')
            ->from('ml_jfpoint_log')
            ->where(array('addtime >=' => $start_time, 'addtime <' =>$end_time, 'adduser' => $uid, 'stage' => '邀请回答'))
            ->count_all_results();

        if (!$add_count) return 0;

        $del_count = $this->db->select('jfpoints')
            ->from('ml_jfpoint_log')
            ->where(array('addtime >=' => $start_time, 'addtime <' =>$end_time, 'adduser' => $uid, 'stage' => '邀请回答失效'))
            ->count_all_results();
        $diff = $add_count - $del_count;

        return $diff * 5;
    }

    //邀请并获得成功回答后得5分
    public function send_invite_jfpoint($uid,$post_id)
    {
        // 邀请回答分数做限制 每天不超过50分
        $today_invite_point = $this->get_today_invite_jf($uid);
        if ($today_invite_point >= 50) {
            return false;
        }

        //获取用户积分
        $uid_jfpoint = $this->get_uid_jfpoints($uid);

        $jfdata = array();
        $jfdata['adduser'] = $uid;
        //$jfdata['dealuser'] = ADMINUSERID;
        $jfdata['beforejfpoints'] = $uid_jfpoint;
        $jfdata['jfpoints'] = 5;
        $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] + 5;
        $jfdata['addtime'] = time();
        $jfdata['stage'] = "邀请回答";
        $jfdata['desc'] = "邀请回答获得5积分";
        $jfdata['related_id'] = $post_id;
        $jfdata['related_type'] = self::RELATED_TYPE_INVITE_SEND;

        $jfpoint_data = $this->insert_jfpoints($jfdata);
        return $jfpoint_data;
    }

    //删除邀请获得的积分
    public function cancel_send_invite_jfpoint($uid,$post_id)
    {
        //获取用户积分
        $uid_jfpoint = $this->get_uid_jfpoints($uid);

        $jfdata = array();
        $jfdata['adduser'] = $uid;
        //$jfdata['dealuser'] = ADMINUSERID;
        $jfdata['beforejfpoints'] = $uid_jfpoint;
        $jfdata['jfpoints'] = -5;
        $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] - 5;
        $jfdata['addtime'] = time();
        $jfdata['stage'] = "邀请回答失效";
        $jfdata['desc'] = "取消邀请回答减5积分";
        $jfdata['related_id'] = $post_id;
        $jfdata['related_type'] = self::RELATED_TYPE_INVITE_SEND_CANCEL;

        $jfpoint_data = $this->insert_jfpoints($jfdata);
        return $jfpoint_data;
    }
    //接受楼主邀请回答获得积分
    public function accept_invite_jfpoint($uid,$post_id)
    {
        //获取用户积分
        $uid_jfpoint = $this->get_uid_jfpoints($uid);

        $jfdata = array();
        $jfdata['adduser'] = $uid;
        //$jfdata['dealuser'] = ADMINUSERID;
        $jfdata['beforejfpoints'] = $uid_jfpoint;
        $jfdata['jfpoints'] = 5;
        $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] + 5;
        $jfdata['addtime'] = time();
        $jfdata['stage'] = "接受邀请回答";
        $jfdata['desc'] = "接受邀请回答获得5积分";
        $jfdata['related_id'] = $post_id;
        $jfdata['related_type'] = self::RELATED_TYPE_INVITE_ACCEPT;
        $jfpoint_data = $this->insert_jfpoints($jfdata);
        return $jfpoint_data;
    }
    //删除接受楼主邀请回答获得积分
    public function cancel_accept_invite_jfpoint($uid,$post_id)
    {
        //获取用户积分
        $uid_jfpoint = $this->get_uid_jfpoints($uid);

        $jfdata = array();
        $jfdata['adduser'] = $uid;
        //$jfdata['dealuser'] = ADMINUSERID;
        $jfdata['beforejfpoints'] = $uid_jfpoint;
        $jfdata['jfpoints'] = -5;
        $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] - 5;
        $jfdata['addtime'] = time();
        $jfdata['stage'] = "接受邀请回答失效";
        $jfdata['desc'] = "取消接受邀请回答获得5积分";
        $jfdata['related_id'] = $post_id;
        $jfdata['related_type'] = self::RELATED_TYPE_INVITE_ACCEPT_CANCEL;

        $jfpoint_data = $this->insert_jfpoints($jfdata);
        return $jfpoint_data;
    }
    
    //帖子被选中发push给积分
    public function marked_push_jfpoint($post_id)
    {
        $this->load->model('postm');
        $author_id =  $this->postm->get_post_author_id($post_id);
        
        if (!$author_id || !$post_id) {
            return false;
        }
        
        $is_exist = $this->exist_record_related_id($author_id, $post_id, self::RELATED_TYPE_MARKED_PUSH);
        if ($is_exist) {
            return false;
        }
        
        //获取用户积分
        $uid_jfpoint = $this->get_uid_jfpoints($author_id);

        $jfdata = array();
        $jfdata['adduser'] = $author_id;
        //$jfdata['dealuser'] = ADMINUSERID;
        $jfdata['beforejfpoints'] = $uid_jfpoint;
        $jfdata['jfpoints'] = 2;
        $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] + 2;
        $jfdata['addtime'] = time();
        $jfdata['stage'] = "帖子被选为推送帖";
        $jfdata['desc'] = "帖子被选为推送帖获得2积分";
        $jfdata['related_id'] = $post_id;
        $jfdata['related_type'] = self::RELATED_TYPE_MARKED_PUSH;
        print_r($jfdata);
        #$jfpoint_data = $this->insert_jfpoints($jfdata);

        //发消息 发push
        $send_content = "[user:" . $author_id . ": ]您的帖子被选为优质帖子推送给了相关科室用户，获得了2积分";
        $this->messagem->send_message($post_id, $author_id, $send_content,$author_id);

        $message = '您的帖子被选为优质帖子推送给了相关科室用户，获得了2积分';
        $extras = array('post_id'=>strval($post_id));

        $this->pushm->sendByUserID($author_id,$message,$extras);
        
        return true;
    }
	//收藏得1个积分
	public function favorite_jfpoint($uid)
	{
		//获取用户积分
		$uid_jfpoint = $this->get_uid_jfpoints($uid);

		$jfdata = array();
        $jfdata['adduser'] = $uid;
        //$jfdata['dealuser'] = ADMINUSERID;
        $jfdata['beforejfpoints'] = $uid_jfpoint;
        $jfdata['jfpoints'] = 1;
        $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] + 1;
        $jfdata['addtime'] = time();
        $jfdata['stage'] = "收藏";
        $jfdata['desc'] = "3个收藏一个积分";

        $jfpoint_data = $this->insert_jfpoints($jfdata);
        return $jfpoint_data;
	}

    //收藏每满30个 加10个积分 石武浩
    public function favorite_jfpoint_10($uid)
    {
        //获取用户积分
        $uid_jfpoint = $this->get_uid_jfpoints($uid);

        $jfdata = array();
        $jfdata['adduser'] = $uid;
        //$jfdata['dealuser'] = ADMINUSERID;
        $jfdata['beforejfpoints'] = $uid_jfpoint;
        $jfdata['jfpoints'] = 10;
        $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] + 10;
        $jfdata['addtime'] = time();
        $jfdata['stage'] = "收藏";
        $jfdata['desc'] = "每30个收藏另加10个积分";

        $jfpoint_data = $this->insert_jfpoints($jfdata);
        return $jfpoint_data;
    }


    //取消收藏减分
    public function favorite_jfpoint_cancel($uid)
    {
        //积分可以为空
        //获取用户积分
        $uid_jfpoint = $this->get_uid_jfpoints($uid);

            $jfdata = array();
            $jfdata['adduser'] = $uid;
            //$jfdata['dealuser'] = ADMINUSERID;
            $jfdata['beforejfpoints'] = $uid_jfpoint;
            $jfdata['jfpoints'] = -1;
            $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] - 1;
            $jfdata['addtime'] = time();
            $jfdata['stage'] = "取消收藏";
            $jfdata['desc'] = "被取消后减去一个积分";

            $jfpoint_data = $this->insert_jfpoints($jfdata);
            return $jfpoint_data;

        return array();
    }


	//签到送积分
	public function user_check_jfpoint($uid)
	{
		$now_time = time();
		if($now_time > 1443283200.0 && $now_time < 1444233600.0)  //2015年 9月 26日 0时 0分 0秒 到 2015年 10月 8日 0时 0分 0秒
        {
			//获取用户积分
			$uid_jfpoint = $this->get_uid_jfpoints($uid);

			$jfdata = array();
	        $jfdata['adduser'] = $uid;
	        //$jfdata['dealuser'] = ADMINUSERID;
	        $jfdata['beforejfpoints'] = $uid_jfpoint;
	        $jfdata['jfpoints'] = 2;
	        $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] + 2;
	        $jfdata['addtime'] = time();
	        $jfdata['stage'] = "节日签到";
	        $jfdata['desc'] = "每日签到";

	        $jfpoint_data = $this->insert_jfpoints($jfdata);
	        return $jfpoint_data;
		}
		else
		{
			//获取用户积分
			$uid_jfpoint = $this->get_uid_jfpoints($uid);

			$jfdata = array();
	        $jfdata['adduser'] = $uid;
	        //$jfdata['dealuser'] = ADMINUSERID;
	        $jfdata['beforejfpoints'] = $uid_jfpoint;
	        $jfdata['jfpoints'] = 1;
	        $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] + 1;
	        $jfdata['addtime'] = time();
	        $jfdata['stage'] = "签到";
	        $jfdata['desc'] = "每日签到";

	        $jfpoint_data = $this->insert_jfpoints($jfdata);
	        return $jfpoint_data;
		}
		
	}

	//连续10次签到送积分
	public function user_check_3_jfpoint($uid)
	{
            //连续10天每日登陆  5积分  不可逆  不可取消
			//获取用户积分
			$uid_jfpoint = $this->get_uid_jfpoints($uid);

			$jfdata = array();
	        $jfdata['adduser'] = $uid;
	        //$jfdata['dealuser'] = ADMINUSERID;
	        $jfdata['beforejfpoints'] = $uid_jfpoint;
	        $jfdata['jfpoints'] = 5;
	        $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] + 5;
	        $jfdata['addtime'] = time();
	        $jfdata['stage'] = "连续签到";
	        $jfdata['desc'] = "连续10天每日登陆";

	        $jfpoint_data = $this->insert_jfpoints($jfdata);
	        return $jfpoint_data;

	}

	//上传头像得10个积分
	public function upload_head_ico_jfpoint($uid)
	{
		//获取用户积分
		$uid_jfpoint = $this->get_uid_jfpoints($uid);
		$jfdata = array();
        $jfdata['adduser'] = $uid;
        //$jfdata['dealuser'] = ADMINUSERID;
        $jfdata['beforejfpoints'] = $uid_jfpoint;
        $jfdata['jfpoints'] = 10;
        $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] + 10;
        $jfdata['addtime'] = time();
        $jfdata['stage'] = "上传头像";
        $jfdata['desc'] = "上传头像得10个积分";

        $jfpoint_data = $this->insert_jfpoints($jfdata);
        return $jfpoint_data;
	}
    //选取最佳答案送5分
    public function set_best_answer_jfpoint($uid,$post_id)
    {
        //获取用户积分
        $uid_jfpoint = $this->get_uid_jfpoints($uid);
        $jfdata = array();
        $jfdata['adduser'] = $uid;
        //$jfdata['dealuser'] = ADMINUSERID;
        $jfdata['beforejfpoints'] = $uid_jfpoint;
        $jfdata['jfpoints'] = 5;
        $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] + 5;
        $jfdata['addtime'] = time();
        $jfdata['stage'] = "选取最佳答案";
        $jfdata['desc'] = "选取最佳答案得5个积分";
        $jfdata['related_id'] = $post_id;
        $jfdata['related_type'] = self::RELATED_TYPE_BESTANSWER_SET;

        $jfpoint_data = $this->insert_jfpoints($jfdata);
        return $jfpoint_data;
    }
    public function cancel_set_best_answer_jfpoint($uid,$post_id)
    {
        $this->load->model('postm');
        $post_info = $this->postm->get_post_info($post_id);

        if($uid != $post_info['author_id'])
        {
            //获取用户积分
            $uid_jfpoint = $this->get_uid_jfpoints($uid);
            $jfdata = array();
            $jfdata['adduser'] = $uid;
            $jfdata['beforejfpoints'] = $uid_jfpoint;
            $jfdata['jfpoints'] = -5;
            $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] - 5;
            $jfdata['addtime'] = time();
            $jfdata['stage'] = "选取最佳答案失效";
            $jfdata['desc'] = "取消选取最佳答案得5个积分";
            $jfdata['related_id'] = $post_id;
            $jfdata['related_type'] = self::RELATED_TYPE_BESTANSWER_CANCEL;

            $jfpoint_data = $this->insert_jfpoints($jfdata);
            return $jfpoint_data;
        }

    }
    //最佳答案送5分
    public function best_answer_jfpoint($uid)
    {
        //获取用户积分
        $uid_jfpoint = $this->get_uid_jfpoints($uid);
        $jfdata = array();
        $jfdata['adduser'] = $uid;
        //$jfdata['dealuser'] = ADMINUSERID;
        $jfdata['beforejfpoints'] = $uid_jfpoint;
        $jfdata['jfpoints'] = 5;
        $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] + 5;
        $jfdata['addtime'] = time();
        $jfdata['stage'] = "最佳答案";
        $jfdata['desc'] = "被选为最佳答案得5个积分";

        $jfpoint_data = $this->insert_jfpoints($jfdata);
        return $jfpoint_data;
    }
    //取消最佳答案减分
    public function cancel_best_answer_jfpoint($uid)
    {
        if(!$uid)
        {
            return array();
        }
        //获取用户积分
        $uid_jfpoint = $this->get_uid_jfpoints($uid);
        $jfdata = array();
        $jfdata['adduser'] = $uid;
        //$jfdata['dealuser'] = ADMINUSERID;
        $jfdata['beforejfpoints'] = $uid_jfpoint;
        $jfdata['jfpoints'] = -5;
        $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] - 5;
        $jfdata['addtime'] = time();
        $jfdata['stage'] = "取消最佳答案";
        $jfdata['desc'] = "被取消为最佳答案减5个积分";

        $jfpoint_data = $this->insert_jfpoints($jfdata);
        return $jfpoint_data;
    }
    public function first_post_jfpoint($uid)
    {
        //获取用户积分
        $uid_jfpoint = $this->get_uid_jfpoints($uid);
        $jfdata = array();
        $jfdata['adduser'] = $uid;
        //$jfdata['dealuser'] = ADMINUSERID;
        $jfdata['beforejfpoints'] = $uid_jfpoint;
        $jfdata['jfpoints'] = 20;
        $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] + 20;
        $jfdata['addtime'] = time();
        $jfdata['stage'] = "首次发帖";
        $jfdata['desc'] = "首次发帖得20个积分";

        $jfpoint_data = $this->insert_jfpoints($jfdata);
        return $jfpoint_data;
    }
    //首次所发帖子被删除
    public function del_first_post_jfpoint($uid)
    {
        //获取用户积分
        $uid_jfpoint = $this->get_uid_jfpoints($uid);
        $jfdata = array();
        $jfdata['adduser'] = $uid;
        //$jfdata['dealuser'] = ADMINUSERID;
        $jfdata['beforejfpoints'] = $uid_jfpoint;
        $jfdata['jfpoints'] = -20;
        $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] - 20;
        $jfdata['addtime'] = time();
        $jfdata['stage'] = "首帖被删除";
        $jfdata['desc'] = "首帖被删除减20个积分";

        $jfpoint_data = $this->insert_jfpoints($jfdata);
        return $jfpoint_data;
    }

    public function post_jfpoint_normal($uid,$post_id,$add_jf)
    {
        //获取用户积分
        $uid_jfpoint = $this->get_uid_jfpoints($uid);
        $jfdata = array();
        $jfdata['adduser'] = $uid;
        //$jfdata['dealuser'] = ADMINUSERID;
        $jfdata['beforejfpoints'] = $uid_jfpoint;
        $jfdata['jfpoints'] = $add_jf;
        $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] + $add_jf;
        $jfdata['addtime'] = time();
        $jfdata['stage'] = "发帖";
        $jfdata['desc'] = "发帖得".$add_jf."个积分";
        $jfdata['related_id'] = $post_id;

        $jfdata['related_type'] = self::RELATED_TYPE_POST;

        $jfpoint_data = $this->insert_jfpoints($jfdata);
        return $jfpoint_data;
    }
    public function del_post_jfpoint_noraml($uid, $post_id)
    {
        $reord = $this->reord_on_related($uid, $post_id, self::RELATED_TYPE_POST);
        if(!$reord)
        {
//            $this->del_post_jfpoint($uid, $post_id);
            return;
        }

        //获取用户积分
        $del_jf = $reord['jfpoints'];
        $uid_jfpoint = $this->get_uid_jfpoints($uid);
        $jfdata = array();
        $jfdata['adduser'] = $uid;
        //$jfdata['dealuser'] = ADMINUSERID;
        $jfdata['beforejfpoints'] = $uid_jfpoint;
        $jfdata['jfpoints'] = -1*($del_jf);
        $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] - $del_jf;
        $jfdata['addtime'] = time();
        $jfdata['stage'] = "发帖删除";
        $jfdata['desc'] = "发帖删除减".$del_jf."个积分";
        $jfdata['related_id'] = $post_id;

        if($reord)
        {
            $jfdata['related_type'] = self::RELATED_TYPE_POST_CANCEL;
        }
        $jfpoint_data = $this->insert_jfpoints($jfdata);
        return $jfpoint_data;
    }
    //节日发帖得5分
    public function post_jfpoint($uid,$post_id)
    {
        $this->load->model('userm');
        $result = $this->userm->getUserInfoById($uid);
        $user_info = $result;
        if($user_info['is_show_truename'])
        {
            $factor = 2;
        }
        else
        {
            $factor = 1;
        }
        //获取用户积分
        $uid_jfpoint = $this->get_uid_jfpoints($uid);
        $jfdata = array();
        $jfdata['adduser'] = $uid;
        //$jfdata['dealuser'] = ADMINUSERID;
        $jfdata['beforejfpoints'] = $uid_jfpoint;
        $jfdata['jfpoints'] = 10 * $factor;
        $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] + 10 * $factor;
        $jfdata['addtime'] = time();
        $jfdata['stage'] = "发帖";
        $jfdata['desc'] = "发帖得".(10*$factor)."个积分";
        $jfdata['related_id'] = $post_id;
        if($user_info['is_show_truename'])
        {
            $jfdata['related_type'] = self::RELATED_TYPE_REALNAME_POST;
        }
        $jfpoint_data = $this->insert_jfpoints($jfdata);
        return $jfpoint_data;
    }
    //节日所发帖子被删减分
    public function del_post_jfpoint($uid, $post_id)
    {
        $reord = $this->reord_on_related($uid, $post_id, self::RELATED_TYPE_REALNAME_POST);
        if($reord)
        {
            $factor = 2;
        }
        else
        {
            $factor = 1;
        }
        //获取用户积分
        $uid_jfpoint = $this->get_uid_jfpoints($uid);
        $jfdata = array();
        $jfdata['adduser'] = $uid;
        //$jfdata['dealuser'] = ADMINUSERID;
        $jfdata['beforejfpoints'] = $uid_jfpoint;
        $jfdata['jfpoints'] = -10*$factor;
        $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] - 10*$factor;
        $jfdata['addtime'] = time();
        $jfdata['stage'] = "发帖删除";
        $jfdata['desc'] = "发帖删除减".(10*$factor)."个积分";
        $jfdata['related_id'] = $post_id;

        if($reord)
        {
            $jfdata['related_type'] = self::RELATED_TYPE_REALNAME_POST_CANCEL;
        }
        $jfpoint_data = $this->insert_jfpoints($jfdata);
        return $jfpoint_data;
    }

    /**
     * 更改评论积分 石武浩 1223
     * @param int $uid
     * @param int $points 积分数量 删除时积分为
     * @param string $type add 增加积分 del 删除积分
     */
    public function change_comment_jfpoint($uid = 0, $type = 'add')
    {
        //获取用户积分
        $uid_jfpoint = $this->get_uid_jfpoints($uid);
        $jfdata = array();
        $jfdata['adduser']        = $uid;
        //$jfdata['dealuser']     = ADMINUSERID;
        $jfdata['beforejfpoints'] = $uid_jfpoint;
        $jfdata['jfpoints']       = $type == 'add' ? 5 : -5;
        $jfdata['resultjfpoints'] = $type == 'add' ? $jfdata['beforejfpoints'] + 5 : $jfdata['beforejfpoints'] - 5;
        $jfdata['addtime']        = time();
        $jfdata['stage']          = $type == 'add' ? "评论满30加5积分"            : '删评论不足30减5积分';
        $jfdata['desc']           = $type == 'add' ? "发评论每满30加5积分"         : '删除评论后不满30减5积分';

        $jfpoint_data = $this->insert_jfpoints($jfdata);
        return $jfpoint_data;
    }

    //50字以上评论给予积分  非实名 1积分  实名 2积分
    public function over_50_commnet_jfpoint($uid,$comment_jfpoint)
    {
        //获取用户积分
        $uid_jfpoint = $this->get_uid_jfpoints($uid);
        $jfdata = array();
        $jfdata['adduser'] = $uid;
        //$jfdata['dealuser'] = ADMINUSERID;
        $jfdata['beforejfpoints'] = $uid_jfpoint;
        $jfdata['jfpoints'] = $comment_jfpoint;
        $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] + $comment_jfpoint;
        $jfdata['addtime'] = time();
        $jfdata['stage'] = "发评论";
        $jfdata['desc'] = "发评论得".$comment_jfpoint."个积分";

        $jfpoint_data = $this->insert_jfpoints($jfdata);
        return $jfpoint_data;
    }


    //节日发评论加积分
    public function commnet_jfpoint($uid)
    {
        //获取用户积分
        $uid_jfpoint = $this->get_uid_jfpoints($uid);
        $jfdata = array();
        $jfdata['adduser'] = $uid;
        //$jfdata['dealuser'] = ADMINUSERID;
        $jfdata['beforejfpoints'] = $uid_jfpoint;
        $jfdata['jfpoints'] = 1;
        $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] + 1;
        $jfdata['addtime'] = time();
        $jfdata['stage'] = "发评论";
        $jfdata['desc'] = "发评论得1个积分";

        $jfpoint_data = $this->insert_jfpoints($jfdata);
        return $jfpoint_data;
    }
    //节日发评论被删除减分
    public function del_commnet_jfpoint($uid)
    {
        $this->load->model('userm');
        $result = $this->userm->getUserInfoById($uid);
        $user_info = $result;
        if($user_info['is_show_truename'])
        {
            $factor = 2;
        }
        else
        {
            $factor = 1;
        }
        //获取用户积分
        $uid_jfpoint = $this->get_uid_jfpoints($uid);
        $jfdata = array();
        $jfdata['adduser'] = $uid;
        //$jfdata['dealuser'] = ADMINUSERID;
        $jfdata['beforejfpoints'] = $uid_jfpoint;
        $jfdata['jfpoints'] = - ($factor);
        $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] - $factor;
        $jfdata['addtime'] = time();
        $jfdata['stage'] = "发评论被删除";
        $jfdata['desc'] = "发评论被删除减".$factor."个积分";

        $jfpoint_data = $this->insert_jfpoints($jfdata);
        return $jfpoint_data;
    }
	//获取单个用户的积分详情
	public function get_details_4_one_user($uid,$page_size,$page)
	{
		$start = ($page-1)*$page_size;
		$result = $this->db->select("beforejfpoints,jfpoints,resultjfpoints,stage,addtime")->where("adduser",$uid)->order_by("id","desc")->limit($page_size,$start)->get("ml_jfpoint_log")->result_array();
		$return = array();
		foreach ($result as $value) {
			$item = array();
			$item['beforejfpoints'] = $value['beforejfpoints'];
			$item['jfpoints'] = $value['jfpoints'];
			$item['resultjfpoints'] = $value['resultjfpoints'];
			$item['stage'] = $value['stage'];
			$item['addtime'] = date("Y-m-d H:i:s",$value['addtime']);
			$return[] = $item;
		}
		return $return;
	}

	/**
	 * 取消加精
	 *
	 * @param $uid
	 * @return mixed
	 */
	public function cancel_post_digest_jfpoint($uid)
	{
		//获取用户积分
		$uid_jfpoint = $this->get_uid_jfpoints($uid);
		$jfdata = array();
		$jfdata['adduser'] = $uid;
		//$jfdata['dealuser'] = ADMINUSERID;
		$jfdata['beforejfpoints'] = $uid_jfpoint;
		$jfdata['jfpoints'] = -10;
		$resultjfpoints = $jfdata['beforejfpoints'] - 10;
		$jfdata['resultjfpoints'] = ($resultjfpoints>=0) ? $resultjfpoints : 0;
		$jfdata['addtime'] = time();
		$jfdata['stage'] = "取消推精";
		$jfdata['desc'] = "取消推精-10积分";

		$jfpoint_data = $this->insert_jfpoints($jfdata);
		return $jfpoint_data;
	}


	/**
	 * 全部完善资料
	 */
	public function complete_all_profile($uid)
	{
		$jifen = 10;
		$stage = "完善资料";
		$desc = "完善资料";

		//获取用户积分
		$uid_jfpoint = $this->get_uid_jfpoints($uid);

		$jfdata = array();
		$jfdata['adduser'] = $uid;
		$jfdata['beforejfpoints'] = $uid_jfpoint;
		$jfdata['jfpoints'] = $jifen;
		$jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] + $jifen;
		$jfdata['addtime'] = time();
		$jfdata['stage'] = $stage;
		$jfdata['desc'] = $desc;

		$jfpoint_data = $this->insert_jfpoints($jfdata);
		return $jfpoint_data;
	}

    public function exist_record_related_id($uid,$related_id,$type)
    {
        $result = $this->db->select("id")->where("adduser",$uid)->where("related_id",$related_id)->where("related_type",$type)->get("ml_jfpoint_log")->result_array();
        return $result&&$result[0] ? true:false;
    }
    public function reord_on_related($uid,$related_id,$type)
    {
        $result = $this->db->select()->where("adduser",$uid)->where("related_id",$related_id)->where("related_type",$type)->get("ml_jfpoint_log")->result_array();
        return $result&&$result[0] ? $result[0]:array();
    }

    /**
     * 根据兑换码查询相关信息
     * @param $unique_code
     * @return mixed
     */
    public function get_points_card_info($unique_code)
    {
        return $this->db->where('unique_code', $unique_code)->get('ml_points_card')->row_array();
    }

    /**
     * 兑换积分
     * @param $points_card_id
     * @param $user_id
     * @return object
     */
    public function exchange_points_card($points_card_id, $user_id)
    {
        $filter = array('id' => $points_card_id, 'user_id' => 0);
        $data   = array('user_id' => $user_id, 'exchange_time' => date('Y-m-d H:i:s'));
        return $this->db->where($filter)->update('ml_points_card', $data, $filter);
    }

    /**
     * 判断莫个用户是否参与过此活动
     * @param $user_id
     * @param $code
     * @return bool
     */
    public function is_partake_activity($user_id, $code)
    {
        $activity_code = substr($code, 0, 3);
        $filter = array('user_id' => $user_id, 'activity_code' => $activity_code);
        $total = $this->db->where($filter)->count_all_results('ml_points_card');
        if ($total > 0) {
            return true;
        }
        return false;
    }

    /**
     * 添加积分卡积分
     * @param $uid
     * @return object
     */
    public function add_points_card_points($user_id, $points)
    {
        //获取用户积分
        $uid_jfpoint = $this->get_uid_jfpoints($user_id);
        $jfdata = array();
        $jfdata['adduser'] = $user_id;
        //$jfdata['dealuser'] = ADMINUSERID;
        $jfdata['beforejfpoints'] = $uid_jfpoint;
        $jfdata['jfpoints'] = $points;
        $jfdata['resultjfpoints'] = $jfdata['beforejfpoints'] + $points;
        $jfdata['addtime'] = time();
        $jfdata['stage'] = "积分卡兑换";
        $jfdata['desc'] = "积分卡兑换{$points}个积分";
        $jfdata['type'] = "998";

        $jfpoint_data = $this->insert_jfpoints($jfdata);
        return $jfpoint_data;
    }





    //
    //获取某个积分活动积分总数 当天
    public function get_today_jfpoints_count( $uid, $stage, $related_type = 0, $related_id = 0)
    {
        //@TODO所有的次数限制或者上限限制 在积分函数里来做 方便查看和统计
        $start_time = strtotime("today");
        $end_time = strtotime("tomorrow");
        $this->db->select_sum('jfpoints');
        $this->db->where("addtime > ",$start_time);
        $this->db->where("addtime < ",$end_time);
        $this->db->where("adduser",$uid);
        $this->db->where("stage",$stage);
        if($related_type)
        {
            $this->db->where("related_type",$related_type);
        }
        if($related_id)
        {
            $this->db->where("related_id",$related_id);
        }

        $query = $this->db->get('ml_jfpoint_log');
        //echo $this->db->last_query();
        $result = $query->result_array();

        return $result;
    }

    //获取某个积分活动次数 当天
    public function get_today_add_count( $uid, $stage, $related_type = 0, $related_id = 0)
    {
        $start_time = strtotime("today");
        $end_time = strtotime("tomorrow");
        $this->db->from('ml_jfpoint_log');
        $this->db->where("addtime > ",$start_time);
        $this->db->where("addtime < ",$end_time);
        $this->db->where("adduser",$uid);
        $this->db->where("stage",$stage);
        if($related_type)
        {
            $this->db->where("related_type",$related_type);
        }
        if($related_id)
        {
            $this->db->where("related_id",$related_id);
        }

        $result = $this->db->count_all_results();

        return $result;
    }
}
